from operation.HDFSUtil import HDFSSERVER
from operation.BaseUtil import LOG
import pandas as pd
import numpy as np
import json
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
Instantiate MobileNet Operation
算子名称：MobileNet MobileNet
算子描述：MobileNet是谷歌在2017年提出的专注于移动端或者嵌入式设备中的轻量级卷积神经网络，利用了深度可分离卷积减少了计算量。
算子参数: 
0 : input_data_path 文件路径 图片数据文件路径
1 : model MobileNet模型 MobileNet模型选择
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
from operation.pretrained.MobileNet import  *
def mobileNet(input_data_path, output_data_path, model):
    if model == 'null': model = None
    labels, image_paths = HDFSSERVER.load_images(input_data_path)
    ids = np.arange(len(image_paths))
    web_paths = [HDFSSERVER.web_path + "/" + i[5:] for i in image_paths]
    if model == "MobileNet":
        preds, preds_prob = MobileNet(img_paths=image_paths, labels=labels, return_prob=1)
    elif model == "MobileNetV2":
        preds, preds_prob = MobileNetV2(img_paths=image_paths, labels=labels, return_prob=1)

    resultDF = pd.DataFrame([ids, web_paths, labels, preds, preds_prob]).T
    resultDF.columns = ["id", "图片", "标签", "预测", "概率"]
    schema = []
    for i in resultDF.columns:
        schema.append({'column_name' : i, 'column_type': 'float'})

    check_status = HDFSSERVER.save_data(output_data_path, resultDF, data_type="dataframe")
    if check_status == "success":
        LOG.info("MobileNet run success")
    return json.dumps({"type": "dataframe"})